package com.yjt.fresh.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyCommentCenter;
import com.yjt.fresh.api.ApiGetCommentList;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanMyPoints;
import com.yjt.fresh.bean.BeanUserComment;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.nfc.tech.MifareUltralight.PAGE_SIZE;

/**
 * 评价中心
 * Created by mcc on 2018/8/2.
 */

public class AtyCommentCenter extends BaseSimpleActivity {
    private static final int COMMENT_ALREADY = 1;
    private static final int COMMENT_NOT_YET = 0;
    private static final int COMMENT_REQUEST = 1002;
    private int mIsComment = COMMENT_NOT_YET;
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.tvAppAtyCommentCenterWait)
    TextView mTvWait;
    @BindView(R.id.vAppAtyCommentCenterWait)
    View mVWait;
    @BindView(R.id.rlAppAtyCommentCenterWait)
    RelativeLayout mRlWait;
    @BindView(R.id.tvAppAtyCommentCenterAlready)
    TextView mTvAlready;
    @BindView(R.id.vAppAtyCommentCenterAlready)
    View mVAlready;
    @BindView(R.id.rlAppAtyCommentCenterAlready)
    RelativeLayout mRlAlready;
    @BindView(R.id.rvAppAtyCommentCenter)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyCommentCenter)
    SwipeRefreshLayout mRefreshLayout;
    private AdpRvAtyCommentCenter mAdpRvAtyCommentCenter;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_comment_center;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

    }

    @Override
    protected void initEvent() {
        initRV();
        initClick();
        getData();
    }

    HttpOnNextListener<BeanUserComment> mCommentListener = new HttpOnNextListener<BeanUserComment>() {
        @Override
        public void onNext(BeanUserComment beanUserComment) {
            mAdpRvAtyCommentCenter.setEmptyView(R.layout.view_empty);
            mTvWait.setText("待评价("+beanUserComment.getNOCOMMENT()+")");
            mTvAlready.setText("已评价("+beanUserComment.getCOMMENTED()+")");
            if (isLoadMore) {
                setData(false, beanUserComment.getList());
            } else {
                setData(true, beanUserComment.getList());
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {

        }
    };

    private void setData(boolean isRefresh, List<BeanUserComment.BeanUserCommentInfo> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpRvAtyCommentCenter.getData().clear();
            mAdpRvAtyCommentCenter.addData(data);
            mAdpRvAtyCommentCenter.setEnableLoadMore(false);
        } else {
            if (size > 0) {
                mAdpRvAtyCommentCenter.addData(data);
                mAdpRvAtyCommentCenter.loadMoreComplete();
            }else {
                mAdpRvAtyCommentCenter.loadMoreEnd(true);
            }
        }
    }
    private void getData() {
        ApiGetCommentList apiGetCommentList = new ApiGetCommentList(mCommentListener,mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("ISCOMMENT",mIsComment);
        map.put("currentPage",mNextRequestPage);
        apiGetCommentList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetCommentList);
    }

    private void initRV() {
        List<BeanUserComment.BeanUserCommentInfo> commentList = new ArrayList<>();
        mAdpRvAtyCommentCenter = new AdpRvAtyCommentCenter(R.layout.app_recycle_item_aty_comment_center,commentList);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        mRv.setAdapter(mAdpRvAtyCommentCenter);
    }

    private void initClick() {
        mAdpRvAtyCommentCenter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvAppRecycleItemAtyCommentCenterDetailOrComment){
                    if (mIsComment == COMMENT_ALREADY){//查看详情
                        Intent intent = new Intent(mContext,AtyCommentDetail.class);
                        intent.putExtra(MyConstants.COMMENT_BEAN,mAdpRvAtyCommentCenter.getData().get(position));
                        startActivity(intent);
                    }else if (mIsComment == COMMENT_NOT_YET){//去评价
                        Intent intent = new Intent(mContext,AtyComment.class);
                        intent.putExtra(MyConstants.COMMENT_BEAN,mAdpRvAtyCommentCenter.getData().get(position));
                        startActivityForResult(intent,COMMENT_REQUEST);
                    }
                }
            }
        });
        mRlAlready.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvAlready.setTextColor(getResources().getColor(R.color.color_43b624));
                mVAlready.setVisibility(View.VISIBLE);

                mTvWait.setTextColor(getResources().getColor(R.color.color_333333));
                mVWait.setVisibility(View.GONE);
                mIsComment = COMMENT_ALREADY;
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
                mAdpRvAtyCommentCenter.isComment(true);
            }
        });

        mRlWait.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvWait.setTextColor(getResources().getColor(R.color.color_43b624));
                mVWait.setVisibility(View.VISIBLE);

                mTvAlready.setTextColor(getResources().getColor(R.color.color_333333));
                mVAlready.setVisibility(View.GONE);

                mIsComment = COMMENT_NOT_YET;
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
                mAdpRvAtyCommentCenter.isComment(false);
            }
        });


        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAdpRvAtyCommentCenter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        },mRv);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == COMMENT_REQUEST){
            getData();
        }
    }
}
